/*
 * ReceiveValuesTask.c
 *
 *  Created on: 29-06-2012
 *      Author: Mati
 */

#ifndef RECEIVEVALUESTASK_C_
#define RECEIVEVALUESTASK_C_

#include "ApplicationTasks.h"

extern xSemaphoreHandle xUartReceivedSem;

void vReceiveValuesTask(void *pvParameters)
{
	uint8_t fbyte;
	uint8_t sbyte;
	uint8_t valLSB;
	uint8_t valMSB;
	uint8_t TempAndTime[28];
	uint8_t Test[2];

	vSemaphoreCreateBinary( xUartReceivedSem );
	if (xUartReceivedSem == NULL)
	{
		//printf("Semafor = NULL\n\r");
	}

	xSemaphoreTake( xUartReceivedSem, 0 ); 					//pominiecie 1 odczytu
	//printf("Receive Values Task\n\r");

/*
#ifdef	INCLUDE_uxTaskGetStackHighWaterMark
	unsigned portBASE_TYPE uxHighWaterMark;
    // Inspect our own high water mark on entering the task.
	uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
	printf("ReceiveValue Task Stack: %d\n\r", uxHighWaterMark);
#endif
*/

	while (1)
	{
		LED_Toggle(1<<7);
		//printf("Task decode przed sprawdzeniem semafora\n\r");

			LPC_UART1->IER |= (1U<<0);						//wlacz przerwania od RecBuff
			//printf("Przed odebraniem semafora\n\r");
			if (pdTRUE
					== xSemaphoreTake( xUartReceivedSem, (portTickType) portMAX_DELAY))
			{
				//printf("Dostalem semafor\n\r");
				fbyte = UART1_GetByte();
				//printf("Pierwszy bajt to: %X\n\r",fbyte);
				sbyte = UART1_GetByte();
				//printf("Drugi bajt to: %X\n\r",sbyte);
				valLSB = UART1_GetByte();
				valMSB = UART1_GetByte();
				//printf("Mam wartosc: %X %X\n\r", valLSB, valMSB);

				if(PMarker.main == fbyte)
				{
					switch(sbyte)
					{
						case _mode:
							if( valLSB )
							{
								eCurrentMode = PREPARE;
								epState = STAGE1;
							}
							else
							{
								eCurrentMode = WORK;
								ewState = DESORPTION;
							}
						break;
						case _autoMode:
							//printf("AutoMode, value1: %X\n\r",valLSB);
							PVal.autoMode = valLSB;
						break;
						case _userMode:
							//printf("UserMode\n\r");
							PVal.userMode = valLSB;
						break;
						case _speaker:
							//printf("Speaker\n\r");
							PVal.speaker = valLSB;
						break;
						case _debug:
							//printf("Debug\n\r");
							PVal.debug = valLSB;
						break;
						case _dac_voltage:
							PVal.dac_voltage = (uint16_t)((valMSB<<8)|valLSB);
						break;
					}
				}
				else if(PMarker.pid == fbyte)
				{
					switch(sbyte)
					{
						case _kp:
							PVal.kp = ConvertUInt16ToFloat( (uint16_t)((valMSB<<8)|valLSB) );
							//printf("kp: %X, %X\n\r",valLSB,valMSB);
							break;
						case _ki:
							PVal.ki = ConvertUInt16ToFloat( (uint16_t)((valMSB<<8)|valLSB) );
							//printf("ki: %X, %X\n\r",valLSB,valMSB);
							break;
						case _kd:
							PVal.kd = ConvertUInt16ToFloat( (uint16_t)((valMSB<<8)|valLSB) );
							//printf("kd: %X, %X\n\r",valLSB,valMSB);
							break;
						case _prec300:
							PVal.prec300 = valLSB;
							//printf("prec300: %X, %X\n\r",valLSB,valMSB);
							break;
						case _prec20:
							PVal.prec20 = valLSB;
							//printf("prec20: %X, %X\n\r",valLSB,valMSB);
							break;
						case _precUser:
							PVal.precUser = valLSB;
							//printf("userPrec: %X, %X\n\r",valLSB,valMSB);
							break;
					}
				}
				else if(PMarker.prepareMode == fbyte)
				{
					switch(sbyte)
					{
						case _ptime1:
							PVal.ptime1 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptime1: %X, %X\n\r",valLSB, valMSB);
							TempAndTime[0] = valLSB;
							TempAndTime[1] = valMSB;
							break;
						case _ptime2:
							PVal.ptime2 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptime2: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[2] = valLSB;
							TempAndTime[3] = valMSB;
							break;
						case _ptime3:
							PVal.ptime3 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptime3: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[4] = valLSB;
							TempAndTime[5] = valMSB;
//							SaveBytesInEEPROM(TempAndTime, PAddress.addr_ptime1, 6 );
//							uint8_t read = ReadByteFromEEPROM(PAddress.addr_ptime3);
//							UART1_PrintChar(read);
							break;
						case _ptime4:
							PVal.ptime4 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptime4: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[6] = valLSB;
							TempAndTime[7] = valMSB;
							break;
						case _ptime5:
							PVal.ptime5 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptime5: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[8] = valLSB;
							TempAndTime[9] = valMSB;
							break;
						case _ptemp1:
							PVal.ptemp1 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptemp1: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[10] = valLSB;
							TempAndTime[11] = valMSB;
							break;
						case _ptemp2:
							PVal.ptemp2 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptemp2: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[12] = valLSB;
							TempAndTime[13] = valMSB;
							break;
						case _ptemp3:
							PVal.ptemp3 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptemp3: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[14] = valLSB;
							TempAndTime[15] = valMSB;
							break;
						case _ptemp4:
							PVal.ptemp4 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptemp4: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[16] = valLSB;
							TempAndTime[17] = valMSB;
							break;
						case _ptemp5:
							PVal.ptemp5 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("ptemp5: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[18] = valLSB;
							TempAndTime[19] = valMSB;
							break;
					}
				}
				else if(PMarker.workMode == fbyte)
				{
					switch(sbyte)
					{
						case _wtime1:
							PVal.wtime1 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("wtime1: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[20] = valLSB;
							TempAndTime[21] = valMSB;
							break;
						case _wtime2:
							PVal.wtime2 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("wtime2: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[22] = valLSB;
							TempAndTime[23] = valMSB;
							break;
						case _wtemp1:
							PVal.wtemp1 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("wtemp1: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[24] = valLSB;
							TempAndTime[25] = valMSB;
							break;
						case _wtemp2:
							PVal.wtemp2 = (uint16_t)((valMSB<<8)|valLSB);
							//printf("wtemp2: %X, %X\n\r",valLSB,valMSB);
							TempAndTime[26] = valLSB;
							TempAndTime[27] = valMSB;
//							SaveBytesInEEPROM(TempAndTime, PAddress.addr_wtime1, 8 );
//							uint8_t read;
//							read = ReadByteFromEEPROM(PAddress.addr_wtime1); //lsb
//							UART1_PrintChar(read);
							break;
					}
				}
				else if(PMarker.twr == fbyte)
				{
					switch(sbyte)
					{
					case _A_TWR:
						PVal.A_TWR = ConvertUInt16ToFloat( (uint16_t)((valMSB<<8)|valLSB) );
						break;
					case _B_TWR:
						PVal.B_TWR = ConvertUInt16ToFloat( (uint16_t)((valMSB<<8)|valLSB) );
						break;
					}
				}
			}
			UART1_RxFIFO_Reset();
/*
			#ifdef	INCLUDE_uxTaskGetStackHighWaterMark == 1
			 Calling the function will have used some stack space, we would
			therefore now expect uxTaskGetStackHighWaterMark() to return a
			value lower than when it was called on entering the task.
				uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
				printf("Receive Value Task Stack: %d\n\r", uxHighWaterMark);
			#endif
*/
	}
	NVIC_EnableIRQ(TIMER1_IRQn);
}

#endif /* RECEIVEVALUESTASK_C_ */
